package com.yc.cloud.business;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yc.cloud.demo.entity.Demo;
import com.yc.cloud.demo.service.DemoService;
import com.yc.cloud.model.FindDemoPage;
import com.yc.cloud.model.SaveDemo;
import com.yc.cloud.model.UpdateDemo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 杨智杰
 * @date 2021/8/13 14:08
 */
@Service
public class DemoBusiness {

    @Resource
    private DemoService demoService;

    public IPage<Demo> page(FindDemoPage param) {
        return demoService.page(new Page<>(param.getCurrent(), param.getSize()), new QueryWrapper<Demo>().lambda()
                .eq(ObjectUtil.isNotNull(param.getId()), Demo::getId, param.getId())
                .like(StrUtil.isNotBlank(param.getName()), Demo::getName, param.getName())
                .orderByDesc(Demo::getCreateTime));

    }

    public Boolean save(SaveDemo param) {
        //获取当前登录用户
        //sysClient.getLoginUser();
//        Demo demo = Convert.convert(Demo.class, param);
//        demo.setCreateBy(1L).setUpdateBy(1L);
//        return demoService.save(demo);
        return demoService.save((Demo) Convert.convert(Demo.class, param).setCreateBy(1L).setUpdateBy(1L));
    }

    public Boolean update(UpdateDemo param) {
        //获取当前登录用户
        //sysClient.getLoginUser();
        Demo demo = Convert.convert(Demo.class, param);
        demo.setUpdateBy(1L);
        return demoService.updateById(demo);
        //return demoService.updateById((Demo) Convert.convert(Demo.class, param).setUpdateBy(1L));
    }

    public Boolean delete(List<Long> id) {
        return demoService.removeByIds(id);
    }

}
